<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Newsletter extends Controller
{
	private $_template_f;
	private $_lang;
	private $_num_item = 200;
	function __construct()
	{
		parent::Controller();
		$this->_lang = $this->mycommon->getLangCode();
		$this->_template_f = $this->config->item('template_f');
		
		$this->load->language('common', $this->_lang);
		$common_lang = $this->lang->line('common_lang');
		
		
		
		$this->load->model('newsletter/newsletter_model');
	}
	
	function _loadHeader($title = '')
	{
		$header = array();
		$header['title'] = $title == '' ? 'Admarket - Administrator' : $title;
		$header['template_f'] = $this->_template_f;
		
		$header['username'] = '';
		$header['userid'] = '';
		$header['permitid'] = '';
		$header['lang'] = $this->_lang;
		$header['module_name'] = 'newsletter';
		if($this->session->userdata('uname'))
		{
			$header['username'] = $this->session->userdata('uname');
			$header['userid'] = $this->session->userdata('uid');
			$header['permitid'] = $this->session->userdata('permitid');
		}
		// load header
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	
	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		if($this->session->userdata('uname'))
		{
			$data['username'] = $this->session->userdata('uname');
			$data['userid'] = $this->session->userdata('uid');
			$data['permitid'] = $this->session->userdata('permitid');
		}
		$data['common_lang'] = $this->lang->line('common_lang');
		
		$this->load->view($this->_template_f . 'menu_view', $data);
	}
	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
	
	function show()
	{	
		if(!$this->session->userdata('uname'))
		{
			$currUrl = $this->mycommon->getCurrentUrl();
			redirect(site_url('/login'). '/?url=' . urlencode($currUrl));
			die();
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				show_error($common_lang['access_denied']);
				die();
			}
		}
		// load header
		$this->_loadHeader('Admarket - Send newsletter');
		
		// load menu
		$this->_loadMenu();
		
		// load content
		$info = $this->newsletter_model->get_list_user(1, $this->_num_item);
		$data = array();
		$data['iCount'] = $info['iCount'];
		$data['pCount'] = $info['pCount'];
		$data['showItem'] = $this->_num_item;
		$this->load->view($this->_template_f . 'newsletter/show_view', $data);
		
		// load footer
		$this->_loadFooter();
	}
	
	function send($cPage)
	{
		if(!$this->session->userdata('uname'))
		{
			$currUrl = $this->mycommon->getCurrentUrl();
			redirect(site_url('/login'). '/?url=' . urlencode($currUrl));
			die();
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				show_error($common_lang['access_denied']);
				die();
			}
		}
		$data = array();
		$data['start'] = 0;
		$data['end'] = 0;
		
		$cPage = is_numeric($cPage) && $cPage > 0 ? $cPage : -1;
		if($cPage > 0)
		{
			$emailConfig['mailtype'] = 'html';
			$this->load->library('email', $emailConfig);
			$data['cPage'] = $cPage;
			$data['start'] = (($cPage - 1) * $this->_num_item) + 1;
			$data['end'] = ($cPage) * $this->_num_item;
			
			$info = $this->newsletter_model->get_list_user($cPage, $this->_num_item);
			$data['iCount'] = $info['iCount'];
			$data['pCount'] = $info['pCount'];
			
			$data['end'] = $data['end'] < $data['iCount'] ? $data['end'] : $data['iCount'];
			foreach($info['lstUser'] as $item)
			{
				if($item != '' && $this->mycommon->validEmail($item['email_address']))
				{
					$email_data = array();
					$arrServingName = $this->config->item('serving_email_name');
					
					$email_subject = "Admarket - Thông báo điều chỉnh giá";
					$email_content = $this->load->view($this->_template_f . 'emailtpl/email_newsletter_03_2012_view', $email_data, true);						
					
					$this->email->clear();
					$this->email->from($this->config->item('serving_email'), $arrServingName[$this->_lang]);
					
					
					$email_to = $item['email_address'];			
					$this->email->to($email_to);
					$this->email->subject($email_subject);
					$this->email->message($email_content).
					//@$this->email->send();
					$this->email->clear();
				}
			}
			
			// load header
			$this->_loadHeader('Admarket - Send newsletter: ' . $data['start'] . '-' . $data['end']);
			
			// load menu
			$this->_loadMenu();
			
			// load content
			$this->load->view($this->_template_f . 'newsletter/send_view', $data);
			
			// load footer
			$this->_loadFooter();
		}
		else // load fail
		{
			// load header
			$this->_loadHeader('Admarket - Send newsletter error page');
			
			// load menu
			$this->_loadMenu();
			
			// load content
			$this->load->view($this->_template_f . 'newsletter/send_error_view');
			
			// load footer
			$this->_loadFooter();
		}
	}
	
	function send_detail()
	{
		if(!$this->session->userdata('uname'))
		{
			die('not ok');
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				die('not ok');
			}
		}
		$strEmail  = trim($this->input->post('email'));
		$arrEmail = explode(',', $strEmail);
		
		$emailConfig['mailtype'] = 'html';
		$this->load->library('email', $emailConfig);
		foreach($arrEmail as $item)
		{
			if($item != '' && $this->mycommon->validEmail($item))
			{
				$email_data = array();
				$arrServingName = $this->config->item('serving_email_name');
				
				$email_subject = "Test adtimes 10/10/2012";
				
				// create
				$email_content = $this->load->view($this->_template_f . 'emailtpl/email_admicro_newsletter_20121010_view', $email_data, true);						
				
				$this->email->clear();
				$this->email->from($this->config->item('serving_email'), $arrServingName[$this->_lang]);
				
				
				$email_to = $item;			
				$this->email->to($email_to);
					
				$this->email->subject($email_subject);
				$this->email->message($email_content).
				@$this->email->send();
				$this->email->clear();
			}
		}
		echo 'ok';
	}
}